﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vuonthieunhi.Areas.Administration.Models;
using vuonthieunhi.Library;
using vuonthieunhi.Models;
using System.Configuration;

namespace TSV.Areas.Administration.Controllers
{
    public class NewsLetterController : BaseController
    {
        NewsLetterDao NL = new NewsLetterDao();
        public ActionResult GetListJQGrid()
        {

            #region JQGrid Params

            string sortColumn = (Request.Params[Constants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[Constants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[Constants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[Constants.ROW_COUNT]);
            #endregion

            List<sp_GetNewsLetterResult> NLList = NL.GetList().ToList<sp_GetNewsLetterResult>();

            int totalRecords = NLList.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);

            var finalList = NL.Sort(NLList, sortColumn, sortOrder)
                                  .Skip((pageIndex - 1) * rowCount)
                                   .Take(rowCount);
            int j = 0;

            var jsonData = new
            {
                total = totalPages,
                page = pageIndex,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {

                        i = m.ID,
                        cell = new string[] {
                                Convert.ToString(j+=1),
                                m.ID.ToString(), 
                                TooltipLink("/Administration/NewsLetter/Edit/"+m.ID.ToString(),HttpUtility.HtmlEncode(m.Title)),
                                HttpUtility.HtmlEncode(m.CreateDate.ToString(Constants.DATETIME_FORMAT)),
                                Constants.ButtonEdit("/Administration/NewsLetter/Edit/"+m.ID.ToString())+"&nbsp;"+ButtonSendMail(m.ID.ToString()),
                                
                            }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetPartnerList()
        {

            //#region JQGrid Params

            string sortColumn = (Request.Params[Constants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[Constants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[Constants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[Constants.ROW_COUNT]);
            //#endregion
            PartnerDao partnerDao = new PartnerDao();
            List<sp_GetPartnerListResult> NLList = partnerDao.GetList("",0);
            NLList.AddRange(NLList);
            NLList.AddRange(NLList);
            NLList.AddRange(NLList);
            NLList.AddRange(NLList);

            int totalRecords = NLList.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);

            var finalList = NLList;
            int j = 0;

            var jsonData = new
            {
                total = totalPages,
                page = pageIndex,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {

                        i = m.ID,
                        cell = new string[] {
                                Convert.ToString(j+=1),
                                m.ID.ToString(), 
                                m.PartnerName,
                            }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Test(string id)
        {
            return View();
        }
        public string ButtonSendMail(string id)
        {
            
            string onclick =string.Format("sendMail({0})", id);
            string button = string.Format("<input type=\"button\" class=\"icon sendmail\" title=\"Gửi bản tin\" onclick=\"{0}\" />", onclick);
            return button;
        }
        public ActionResult SendMail(string id)
        {
            ViewData["NewsLetterID"] = id;
            return View();
        }
        private string TooltipLink(string actionUrl, string textLink)
        {
            string onclick = string.Format("window.location='{0}';", actionUrl);
            string jsvoid = "javascript:void(0);";
            string str = string.Format("<a id=\"textlnk\" href=\"{0}\" class=\"showTooltip\" onclick=\"{1}\">{2}</a>", jsvoid, onclick, textLink);
            return str;
        }      
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Newsletter ar, FormCollection collection)
        {
            NewsLetterDao dao = new NewsLetterDao();
            Message msg = null;
            JsonResult result = new JsonResult();
            result.ContentType = "text/html";
            result.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
            ar.CreateDate = DateTime.Now;
            ar.IsSent = false;
            msg = dao.Insert(ar);
            ShowMessage(msg);
            result.Data = msg;
            return result;

        }
        public ActionResult DeleteList(string id)
        {
            Message msg = NL.DeleteList(id);
            ShowMessage(msg);
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            Newsletter oscObj = NL.GetById(id.ToString());
            return View(oscObj);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Newsletter oscObj, FormCollection collection)
        {
            NewsLetterDao dao = new NewsLetterDao();
            Message msg = null;
            JsonResult result = new JsonResult();
            result.ContentType = "text/html";
            result.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
            try
            {

                // TODO: Add update logic here
                msg = NL.Update(oscObj);

            }
            catch
            {
                ShowMessage(msg);
                return View();
            }
            ShowMessage(msg);
            result.Data = msg;
            return result;
        }


    }
}
